*===================================================================================*
* Ado-file: 	csxmpnl Version 1.0 
* Author: 		Chen Shao(陈志豪)
* Affiliation: 	Lanzhou University
* E-mail: 		chenzhh20@lzu.edu.cn
* License:      Mulan PSL v2
* Date: 		2025/2/17                                 
*===================================================================================*

capture program drop csxmpnl
program define csxmpnl
	version 16
	syntax varlist [if] [in],id(varname) time(varname)
	preserve
	di in blue "晨韶学妹向您报告："
	local y : word 1 of `varlist'
	foreach var in `varlist'{
		if "`var'" != "`y'"{
			local x `x' `var'
		}
	}
	capture which esttab //检测是否有esttab命令
    if _rc!=0{
        qui ssc install estout,replace
         //下载esttab命令用于输出
    }
	capture which xttest3 //检测是否有xttest3命令
    if _rc!=0{
        qui ssc install xttest3,replace
         //下载xttest3命令用于检验
    }
	capture which xttest2 //检测是否有xttest2命令
    if _rc!=0{
        qui ssc install xttest2,replace
         //下载xttest2命令用于检验
    }
	capture which xtserial //检测是否有xtserial命令
    if _rc!=0{
        qui net from http://www.stata-journal.com/software/sj3-2/
		qui net describe st0039
		net install st0039,replace
         //下载xtserial命令用于检验
    }
	capture which xtcsd //检测是否有xtcsd命令
    if _rc!=0{
        qui ssc install xtcsd,replace
         //下载xtcsd命令用于检验
    }
	
	local id `id'
	local time `time'
	local Panel=1
	while `Panel'==1{
		local model "`y' `x'"
		xtset `id' `time'
		local numt=(r(tmax)+1-r(tmin))/r(tdelta)
		qui xtsum `id'
		local numid=r(n)
		if `numid'>`numt' {
			local dtatyp "短面板"
		}
		if `numid'<=`numt' {
			local dtatyp "长面板"
		}
		di as yellow _n _n "一、变量描述"
		d `model' `id' `time'
        xtdes
		di as white _n "主要变量特征:"
		xtsum `model'
		
		di as yellow _n _n "二、相关性检验:"
		pwcorr `model',star(.05)
		qui pwcorr `model',sig
		mat sig=r(sig)
		mat MATC=r(C)
		foreach var in `x'{
			local p=el(sig,rownumb(sig,"`var'"),1)
			local c=el(MATC,rownumb(MATC,"`var'"),1)
			while `p' < 0.05{
				local result "显著"
				local p=0.05
			}
			while `p' > 0.05{
				local result "不显著"
				local p=0.05
			}
			di in white "`y'和`var'相关系数为" `c' ",`result'"
		}
		
		di as yellow _n _n "三、平稳性检验:" //直接调用csxmpnlurt命令
        di as white "若要检验请点击："
		di `"{stata "csxmpnlurt `model',id(`id') time(`time')"}"'
		
		di as yellow _n _n "四、模型选取:F/Breusch-Pagan/Hausman检验"
		local pmod=0
		local rmod=0
		local fmod=0
		qui xtreg `model',fe
		estimates store FE
		local p_f=e(p_f)
		while `p_f'<0.05{
			local result "FE优于POOL"
			local p_f=0.05
			local fmod=`fmod'+1
		}
		while `p_f'>0.05{
			local result "POOL优于FE"
			local p_f=0.05
			local pmod=`pmod'+1
		}
		di in white "F检验:`result'"
		qui xtreg `model',re r theta
		qui xttest0
		local p=r(p)
		while `p'<0.05{
			local result "RE优于POOL"
			local p=0.05
			local rmod=`rmod'+1
		}
		while `p'>0.05{
			local result "POOL优于RE"
			local p=0.05
			local pmod=`pmod'+1
		}
		di in white "Breusch-Pagan检验:`result'"
		qui xtreg `model',re
		estimates store RE
		qui hausman FE RE,c sig
		local p=r(p)
		while `p'<0.05{
			local result "FE优于RE"
			local p=0.05
			local fmod=`fmod'+1
		}
		while `p'>0.05{
			local result "RE优于FE"
			local p=0.05
			local rmod=`rmod'+1
		}
		di in white "传统Hausman检验:`result'"
		local nmod=0
		drop _est_FE
		drop _est_RE
		
		while `pmod'==2{
			di as white "综上决定采用聚类标准误的混合回归"
			di as yellow _n _n "五、POOL模型实证分析" _n
			qui reg `model'
			estimates store OLS
			qui reg `model',vce(cluster `id')
			estimates store OLS_robust
			esttab OLS OLS_robust,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			qui xtreg `model',re
			estimates store RE
			qui xtreg `model',re r th
			estimates store RE_robust
			qui xtreg `model',fe
			estimates store FE
			qui xtreg `model',fe r
			estimates store FE_robust
			qui xtreg `model',be
			estimates store BE
			local pmod=0
			local nmod=1
		}
		while `rmod'==2{
			di as white "综上决定采用随机效应模型回归"
			di as yellow _n _n "五、RE模型实证分析"
			qui xtreg `model',re
			estimates store RE
			qui xtreg `model',re r th
			estimates store RE_robust
			qui xtreg `model',mle
			estimates store MLE
			esttab RE RE_robust MLE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
            qui reg `model'
			estimates store OLS
			qui reg `model',vce(cluster `id')
			estimates store OLS_robust
			qui xtreg `model',fe
			estimates store FE
			qui xtreg `model',fe r
			estimates store FE_robust
			qui xtreg `model',be
			estimates store BE
			local rmod=0
			local nmod=1
		}
		while `fmod'==2{
			di as white "综上决定采用固定效应模型回归"
			di as yellow _n _n "五、FE模型实证分析"
			qui xtreg `model',fe
			estimates store FE
			qui xtreg `model',fe r
			estimates store FE_robust
			qui reg `model' i.`id',vce(cluster `id')
			estimates store LSDV
			qui xtreg `model' i.`time',fe r
			estimates store FE_TW
			esttab FE FE_robust LSDV FE_TW,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`time'" "*`id'")
			qui reg `model'
			estimates store OLS
			qui reg `model',vce(cluster `id')
			estimates store OLS_robust
			qui xtreg `model',re
			estimates store RE
			qui xtreg `model',re r th
			estimates store RE_robust
			qui xtreg `model',be
			estimates store BE
			local fmod=0
			local nmod=1
		}
		while `nmod'==0{
			di as white "综上无法决定采用哪种模型回归"
			di as yellow _n _n "五、POOL/RE/FE模型实证分析"
			qui reg `model'
			estimates store OLS
			qui reg `model',vce(cluster `id')
			estimates store OLS_robust
			qui xtreg `model',re
			estimates store RE
			qui xtreg `model',re r th
			estimates store RE_robust
			qui xtreg `model',fe
			estimates store FE
			qui xtreg `model',fe r
			estimates store FE_robust
			qui xtreg `model',be
			estimates store BE
			local nmod=1
			esttab OLS OLS_robust RE RE_robust FE FE_robust BE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
			
		}
		
		di as yellow _n _n "六、模型对比展示"
		esttab OLS OLS_robust RE RE_robust FE FE_robust BE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)
		
		if "`dtatyp'" == "长面板" {
			di as yellow _n _n "七、异方差、自相关检验与估计(仅在长面板中分析)"
			qui su `time' 
			g timetrend =`time'-r(min)+1
			di as white _n "(一)异方差、自相关检验"
			qui xtserial `model' `id'*
			local p=r(p)
			while `p'<0.05{
			local result "存在组内一阶自相关"
			local p=0.05
			local raac=1
			}
			while `p'>0.05{
				local result "不存在组内一阶自相关"
				local p=0.05
				local raac=0
			}
			di as white "1 组内自相关检验:`result'"
			
			local erac=0
			qui xtreg `model' i.`time' ,fe
			qui xtcsd,pes
			local p = 1-chi2((e(N_g)-1),r(pesaran)) //由于xtcsd没有提供标量，所以还得自己手动算
			 while `p'<0.05{
			local result "存在组间同期相关"
				local p=0.05
				local erac=1+`erac'
			}
			while `p'>0.05{
				local result "不存在组间同期相关"
				local p=0.05
				local erac=0+`erac'
			}
			di as white "2.1 组间同期相关检验(pesaran):`result'"
			qui xtcsd,fri
			local p = 1-chi2((e(N_g)-1),r(fried)) //由于xtcsd没有提供标量，所以还得自己手动算
			while `p'<0.05{
				local result "存在组间同期相关"
				local p=0.05
				local erac=1+`erac'
			}
			while `p'>0.05{
				local result "不存在组间同期相关"
				local p=0.05
				local erac=0+`erac'
			}
			di as white "2.2 组间同期相关检验(fried):`result'"
			
			qui xtreg `model' timetrend,fe
			qui xttest3
			local p=r(p)
			while `p'<0.05{
				local result "存在组间异方差"
				local p=0.05
				local erhe=1
			}
			while `p'>0.05{
				local result "不存在组间异方差"
				local p=0.05
				local erhe=0
			}
			di as white "3 组间异方差检验:`result'"
			
			di as white _n "(二)PSCE与全面FGLS估计"
			 //PSCE
			if `erhe'==1{
				if `raac'==0{
					qui xtpcse `model' i.`id' timetrend, het
					estimates store PCSE
				}
				if `raac'==1{
					qui xtpcse `model' i.`id' timetrend
					estimates store PCSE
				}
			}
			
			 //FGLS
			while `erhe'==1 & `raac'==0 & `erac'==0{
				qui xtgls `model' i.`id' timetrend,p(h) c(i)
				estimates store FGLS
				qui xtgls `model' i.`id' timetrend,p(h) c(i) igls
				estimates store IFGLS
				esttab FGLS IFGLS PCSE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`id'")
				local erhe=3
			}
			while `erhe'==1 & `raac'==1 & `erac'==0{
				qui xtgls `model' i.`id' timetrend,p(h) c(ar1)
				estimates store FGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(h) c(ar1) igls
				estimates store IFGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(h) c(psar1)
				estimates store FGLS_PSAR1
				qui xtgls `model' i.`id' timetrend,p(h) c(ar1) igls
				estimates store IFGLS_PSAR1
				esttab FGLS_AR1 IFGLS_AR1 FGLS_PSAR1 IFGLS_PSAR1 PCSE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01)  drop("*`id'")
				local erhe=3
			}
			while `erhe'==1 & `raac'==1 & `erac'!=0{
				qui xtgls `model' i.`id' timetrend,p(c) c(ar1)
				estimates store FGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(c) c(ar1) igls
				estimates store IFGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(c) c(psar1)
				estimates store FGLS_PSAR1
				qui xtgls `model' i.`id' timetrend,p(c) c(ar1) igls
				estimates store IFGLS_PSAR1
				esttab FGLS_AR1 IFGLS_AR1 FGLS_PSAR1 IFGLS_PSAR1 PCSE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`id'")
				local erhe=3
			}
			while `erhe'==1 & `raac'==0 & `erac'!=0{
				qui xtgls `model' i.`id' timetrend,p(c) c(i)
				estimates store FGLS
				qui xtgls `model' i.`id' timetrend,p(c) c(i) igls
				estimates store IFGLS
				esttab FGLS IFGLS PCSE,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`id'")
				local erhe=3
			}
			while `erhe'==0 & `raac'==0 & `erac'==0{
				qui xtgls `model' i.`id' timetrend,p(i) c(i)
				estimates store FGLS
				qui xtgls `model' i.`id' timetrend,p(i) c(i) igls
				estimates store IFGLS
				esttab FGLS IFGLS,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`id'")
				local erhe=3
			}
			while `erhe'==0 & `raac'==1 & `erac'==0{
				qui xtgls `model' i.`id' timetrend,p(i) c(ar1)
				estimates store FGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(i) c(ar1) igls
				estimates store IFGLS_AR1
				qui xtgls `model' i.`id' timetrend,p(i) c(psar1)
				estimates store FGLS_PSAR1
				qui xtgls `model' i.`id' timetrend,p(i) c(ar1) igls
				estimates store IFGLS_PSAR1
				esttab FGLS_AR1 IFGLS_AR1 FGLS_PSAR1 IFGLS_PSAR1,b(3) se(2) sca(r2_a N F) nogap brackets compress mtitle nonum star(* 0.1 ** 0.05 *** 0.01) drop("*`id'")
				local erhe=3
			}
			while `erhe' !=3 {
				di as white "无修正方法."
			}
		}
		di as white _n _n "分析结束"
		local Panel=0
	}
	restore
	di in blue "您布置的任务已经完成，请您过目~♥"
end